package com.company;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 代码使用文心一言生成
 *
 * @date : 2023-07-02 下午8:41
 */

public class P061_PrintFilesInDirectory {

    public static class PrintFilesInDirectory {

        public static void main(String[] args) {
            File directory = new File("/Users/lilei/Downloads/豆瓣TOP250");
            List<String> list1 = printFiles(directory);
            List<String> list2 = readFile("file/book.txt", "UTF-8");
            List<String> commonElements = list2.stream()
                    .filter(e -> !list1.contains(e))
                    .collect(Collectors.toList());
            System.out.println(commonElements);
        }

        public static List<String> printFiles(File directory) {
            List<String> ans = new ArrayList<>();
            File[] files = directory.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isFile()) {
                        ans.add(file.getName().substring(0, file.getName().indexOf(".")));
                    } else if (file.isDirectory()) {
                        printFiles(file);
                    }
                }
            }
            return ans;
        }


        private static List<String> readFile(String filename, String encoding) {
            List<String> ans = new ArrayList<>();
            try (BufferedReader buf = new BufferedReader(new InputStreamReader(new FileInputStream(filename), encoding))) {
                String line;
                while ((line = buf.readLine()) != null) {
                    ans.add(line);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return ans;
        }
    }
}
